<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 使用通用工具栏组件 -->
      <page-tools>
        <template #slot-left>
          <span>共17条记录</span>
        </template>

        <template #slot-right>
          <el-button type="danger" size="small">导入excel</el-button>
          <el-button type="success" size="small">导出excel</el-button>
          <el-button type="primary" size="small">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px">
        <el-table border :data="employeesList">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" align="center">
            <template v-slot="{ row }" >
              <!-- http://ihrm.itheima.net/static/img/head.b6c3427d.jpg -->
              <img :src="row.staffPhoto || 'http://ihrm.itheima.net/static/img/head.b6c3427d.jpg' " height="50" alt="">
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="mobile" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" />
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作" width="280">
            <template>
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <!-- 分页区域 -->
          <el-pagination
            :current-page="query.page"
            :page-sizes="[10, 15, 20, 25]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqEmployees } from '@/api/employees'
import { Message } from 'element-ui';

export default {
  data(){
    return{
      query:{
        page:1,
        size:10
      },
      employeesList: [], // 员工列表
      total: 0 // 数据总条数
    }
  },
  methods:{

    // 封装获取员工列表的方法
    async getEmployeesList() {
      try {
        const res = await reqEmployees(this.query)
        this.employeesList = res.data.rows
        this.total = res.data.total
      } catch (e) {
        Message.error('获取员工列表失败')
      }
    },

    // 每页显示的条数发生改变时触发
    handleSizeChange(newSize) {
      this.query.size = newSize
      this.getEmployeesList()
    },

    // 当前页面发生改变时触发
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getEmployeesList()
    }
  },

  created(){
    this.getEmployeesList()
  }
  
};
</script>

<style lang="scss" scoped></style>
